//
//  CreatOrderViewController.m
//  1243
//
//  Created by 袖手旁觀 on 16/4/15.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "CreatOrderViewController.h"
#import "NetWorkTools.h"
#import "ReadLoginData.h"
#import "SVProgressHUD.h"

@interface CreatOrderViewController ()<UIAlertViewDelegate>
/**
 *  收货人
 */
@property (weak, nonatomic) IBOutlet UITextField *ShouHuoRenTextField;

/**
 *  收货人电话
 */
@property (weak, nonatomic) IBOutlet UITextField *ShouHuoRenPhoneNum;

/**
 *  省份
 */
@property (weak, nonatomic) IBOutlet UITextField *ShengFenTextField;

/**
 *  城市
 */
@property (weak, nonatomic) IBOutlet UITextField *CityTextField;

/**
 *  详细地址
 */
@property (weak, nonatomic) IBOutlet UITextField *AddRessTextField;

@property (nonatomic,copy) NSString *OrderNumber;

@end

@implementation CreatOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"创建订单";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self registerTextField];
}

- (void)registerTextField {
    [self.ShouHuoRenTextField resignFirstResponder];
    [self.ShouHuoRenPhoneNum resignFirstResponder];
    [self.ShengFenTextField resignFirstResponder];
    [self.CityTextField resignFirstResponder];
    [self.AddRessTextField resignFirstResponder];
}

- (IBAction)CreatOrderDidClickButton:(id)sender {
    if(!self.AddRessTextField.text.length) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"详细地址不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        
        return;
    }
    
    [self registerTextField];
    
    NSDictionary *loadUserData = [ReadLoginData readLoginData];

    
    NSDictionary *dict = @{
                           @"UserId":[loadUserData objectForKey:@"Number"],
                           @"UserName":[loadUserData objectForKey:@"UserName"],
                           @"ProductId":self.shopID,
                           @"BuyNum":@"1",
                           @"ReceiveName":self.ShouHuoRenTextField.text,
                           @"ReceiveMobile":self.ShouHuoRenPhoneNum.text,
                           @"ReceivePhone":self.ShouHuoRenPhoneNum.text,
                           @"ReveiveProvice":self.ShengFenTextField.text,
                           @"ReceiveCity":self.CityTextField.text,
                           @"ReceiveAddress":self.AddRessTextField.text
                           };
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"CREATEORDER" completeBlock:^(id object) {
       
        NSLog(@"%@",object);
        
        NSDictionary *dict = object;
        
        self.OrderNumber = [dict objectForKey:@"BackData"];
        
//        [ReadLoginData savelogindataToLocal:dict AndPlistPath:@"CreatOrder"];
        
    }];
    
    
    
    [SVProgressHUD show];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SVProgressHUD dismiss];
        
        NSDictionary *dict = [ReadLoginData readLoginData];
        
        NSLog(@"%@",dict);
        
        NSDictionary *params = @{
                                 @"UserId":[dict objectForKey:@"Number"],
                               @"UserName":[dict objectForKey:@"UserName"],
                               @"OrderNumber":self.OrderNumber,
                               @"PayMoneyType":@"3"
                               };
        
        [[NetWorkTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:@"PAYORDER" completeBlock:^(id object) {
            NSLog(@"%@",object)
            
            NSString *string = [object objectForKey:@"Message"];
            
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:string delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 11111;
            [alert show];
        }];
        
    });
    
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 10086 || alertView.tag == 11111) {

        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
